<?php
namespace app\index\controller;

use app\common\controller\HomeBase;  // model
use think\Db;                       // db
use think\Session;
use app\common\model\Wacategory as WacategoryModel;
use app\common\model\Imgcategory as ImgcategoryModel;
use app\common\model\Wenan as WenanModel;
use app\common\model\Title as TitleModel;
use app\common\model\Images as ImagesModel;
use app\common\model\Search as SearchModel;

class Index extends HomeBase
{
    protected $wacategory_model;
    protected $imgcategory_mode;
    protected $wenan_model;
    protected $images_model;
    protected $search_model;
    protected $title_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wacategory_model = new WacategoryModel();
        $this->imgcategory_mode = new ImgcategoryModel();
        $this->wenan_model = new WenanModel();
        $this->images_model = new ImagesModel();
        $this->search_model = new SearchModel;
        $this->title_model = new TitleModel();
        $category_level_list = $this->wacategory_model->getLevelList();
        $imgcategory_level_list = $this->imgcategory_mode->field('id,name')->where('pid',75)->limit(15)->order('click desc')->select();
        // dump($imgcategory_level_list);exit();
        $this->assign('imgcategory_level_list',$imgcategory_level_list);
        $this->assign('category_level_list',$category_level_list);
    }

    public function docs(){
        // 根据分类筛选显示文案  显示  wenan 里面的所有数据 和 分类数据里面的分类名称分开
        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
        $this->assign('wenan_list',$result);
        $this->assign('title','文案列表');
        return $this->fetch();
    }

    public function docst(){
        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
        dump($result);
//        $this->assign('wenan_list',$result);
//        $this->assign('title','文案列表');
//        return $this->fetch();
    }


    // 文案长页面，文案短页面，二维码加粉页，文案弹框，创意图片，推广标题
    public function index()
    {
        $where = [
            'status' => ['eq',1],
        ];
        $article_hot = $this->wacategory_model->field('id,name')->order('click desc')->limit(8)->select();
        $share_hot = $this->images_model->where('examine',1)->where('type',0)->order('readding DESC')->limit(3)->select();
        $ori_hot = $this->images_model->where('examine',1)->where('type',1)->order('readding DESC')->limit(4)->select();
        $int_hot = $this->images_model->where('examine',1)->where('type',2)->order('readding DESC')->limit(4)->select();
        $article_hot_list = $this->wenan_model->where($where)->order('readding DESC')->limit(4)->select();
        $rec = $this->search_model->field('text')->where('rec',1)->limit(7)->order('num DESC')->select();
        $news_count = $this->news_count();
        $media_count = $this->media_count();
        $count = array_merge($news_count,$media_count);
        $this->assign('count',$count);
        $this->assign('share_hot',$share_hot);
        $this->assign('ori_hot',$ori_hot);
        $this->assign('int_hot',$int_hot);
        $this->assign('article_hot',$article_hot);
        $this->assign('article_hot_list',$article_hot_list);
        $this->assign('title','首页');
        $this->assign('nav',0);
        $this->assign('rec',$rec);
        return $this->fetch();
    }

    //文案列表统计
    public function news_count(){
        $news_pid = $this->wacategory_model->field('id,name')->where('pid',0)->select();
        $count = [];
        foreach($news_pid as $pid){
            $news_id = $this->wacategory_model->where('pid',$pid['id'])->column('id');
            // $news_sid = [];
            // foreach($news_id as $id){
            //     $arr = $this->wacategory_model->where('pid',$id)->column('id');
            //     $news_sid = array_merge($news_sid,$arr);
            // }
            $count[] = [
                'name' => $pid['name'],
                'count' => $this->wenan_model->whereIn('wid',$news_id)->count(),
            ];
        }

        return $count;
    }

    //标题统计
    public function title_count(){
        $this->title_model->field('id,name')->where('pid',0)->select();
    }

    //图片素材列表统计
    public function media_count(){
        $news_pid = $this->imgcategory_mode->field('id,name')->where('pid',0)->select();
        $count = [];
        foreach($news_pid as $pid){
            $news_id = $this->imgcategory_mode->where('pid',$pid['id'])->column('id');
            // $news_sid = [];
            // foreach($news_id as $id){
            //     $arr = $this->imgcategory_mode->where('pid',$id)->column('id');
            //     if(empty($arr)){
            //         $news_sid = array_merge($news_sid,[$id]);
            //     }else{
            //         $news_sid = array_merge($news_sid,$arr);
            //     }
            // }
            if($pid['id'] == '76'){
                $count[] = [
                    'name' => $pid['name'],
                    'count' => $this->title_model->where('examine',1)->where('status',1)->whereIn('pid',$news_id)->count(),
                ];
            }else if($pid['id'] == '75'){
                $count[] = [
                    'name' => $pid['name'],
                    'count' => $this->images_model->where('examine',1)->where('status',1)->whereIn('pid',$news_id)->count(),
                ];
            }
        }
        return $count;
    }

    public function hotarticlelist(){
        if($this->request->isPost()){
            $id = $this->request->post('id');
            $where = [
                'status' => ['eq',1],
            ];
            // $ids = $this->wacategory_model->where('pid',$id)->column('id');
            $hotarticlelist = $this->wenan_model->where($where)->where('wid',$id)->order('readding DESC')->limit(5)->select();
            $this->assign('hotarticlelist',$hotarticlelist);
            return $this->fetch();
        }
    }

    public function i( $keyword = '', $page = 1){
        $map   = [];
        $field = 'id,title,wid,author,reading,status,publish_time,sort';

        // 筛选分类
        if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }

        $result = $this->wenan_model->field($field)->where($map)->order(['publish_time' => 'DESC'])->paginate(15, false, ['page' => $page]);

        $wecategory = $this->wacategory_model->column('name', 'id');

//        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
//        $this->assign('wenan_list',$result);
//        $this->assign('title','文案列表');
        return $this->fetch('i', ['wecategory_list' => $wecategory, 'wenan_list' => $result, 'keyword' => $keyword,'title','文案列表']);
    }
}
/*
 * CREATE TABLE `think_wenan` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `wid` int(11) NOT NULL,
  `wenan_path` varchar(255) NOT NULL,
  `compress` varchar(255) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `thumb` varchar(255) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `sort` int(11) DEFAULT '0',
  `create_time` datetime NOT NULL,
  `status` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8;
*/






